package cn.wolfcode.spring_producer._01_hello;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class produce {
    @Autowired

    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("01-hello")
    public String sendMsg(String message) throws Exception{
        SendResult sendResult = rocketMQTemplate.syncSend("01-boot", message);

        return "success";

    }
    @RequestMapping("03-hello")
    public String sendMsg3(String msg) throws Exception{
        rocketMQTemplate.sendOneWay("01-boot", msg);
        return "success";

    }

    @RequestMapping("02-hello")
    public String sendMsg2(String msg) {

        rocketMQTemplate.asyncSend("01-boot", msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消息消费成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("消息消费失败");
            }
        });



        return "success";

    }


    @RequestMapping("04-hello")
    public String sendMsg4(String msg) throws Exception{
        MessageBuilder<String> bulid = MessageBuilder.withPayload(msg);
        Message<String> message = bulid.build();

        SendResult sendResult = rocketMQTemplate.syncSend("01-boot",message ,3000,3);

        return "success";

    }

    @RequestMapping("01-hello")

    public String sendMsg(String message,String age) throws Exception{

        SendResult sendResult = rocketMQTemplate.syncSend("01-boot:", message);

        System.out.println(sendResult.getMsgId());

        System.out.println(sendResult.getSendStatus());

        return "success";

    }

}
